import { SubscribeMessage, WebSocketGateway, WsResponse, WebSocketServer, ConnectedSocket, MessageBody} from '@nestjs/websockets';
import { Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { Server, Client, Socket } from 'socket.io';
import { DefaultConfig } from '../common/define';

@WebSocketGateway(DefaultConfig.Socket.prot, {namespace: DefaultConfig.Socket.namespace})
export class EventsGateway {

  @WebSocketServer() server;
  //#region 生命周期事件
  // 初始化
  // afterInit(server: Server) {
  //   console.log('start-server-init', server);
  // }

  // // 连接
  // handleConnection(client: Client) {
  //   console.log('start-server-connection', client);
  // }

  // // 断开连接
  // handleDisconnect(client: Client) {
  //   console.log('start-server-disconnect', client);
  // }

  // 注册事件
  @SubscribeMessage('events')
  handleEvent(@MessageBody() data: string, @ConnectedSocket() client: Socket) {
    return of({
        event: 'events',
        data: {
          msg: data,
        },
    });
  }
}
